﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Summer.IService;
using Summer.Model;
using Summer.Redis;

namespace Summer.WebApi.Api
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        public IUserService users { get; set; }   //属性注入
        private IUserService _baseService;
        public UserController(IUserService userService)
        {
            _baseService = userService;
        }

        /// <summary>
        /// 获取用户集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<Users> UserList()
        {
            return _baseService.GetUsers();
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        [HttpGet]
        public Users UserInfo(int userId)
        {
            return _baseService.GetUser(userId);
        }

        [HttpGet]
        public IEnumerable<Users> list()
        {
            return _baseService.GetList();
        }

        [HttpGet]
        public IEnumerable<Users> list2()
        {
            //属性注入测试
            return users.GetList();
        }

        [HttpGet]
        public Users info(int userId)
        {
            return _baseService.Get(userId);
        }

        [HttpGet]
        public Users GetUserCache(int userId)
        {
            Users user= RedisHelper.Instance.StringGet<Users>(RedisKeyCommon.userKey + userId.ToString());
            if (user == null)
            {
                Users userInfo = _baseService.Get(userId);
                RedisHelper.Instance.StringSet<Users>(RedisKeyCommon.userKey + userId.ToString(), userInfo);
            }
            return user;
        }


    }
}